package com.flex.showdoc4j.factory;

import com.flex.core.annotation.Doc;
import com.flex.core.annotation.DocParam;
import com.flex.core.enums.ParamType;
import com.flex.core.enums.RequestMethod;
import com.flex.core.enums.Required;
import com.flex.showdoc4j.model.DocModel;
import com.flex.showdoc4j.model.ParamModel;

/**
 * @author ganlt<br>
 * @since 2019/06/26 15:42 <br>
 */
public class DocFactory {
    /**
     * 创建doc模板类
     * @param doc 基本文档注解
     * @return doc模板类
     */
    public static DocModel createDocModel(Doc doc) {
        String desc = doc.desc();
        String title = doc.title();
        String url = doc.url();
        boolean isPage = doc.isPage();
        int[] pager = doc.pager();

        String result = doc.result();
        RequestMethod requestMethod = doc.method();

        if (pager.length != 2) {
            throw new RuntimeException("分页参数有且只有两个, [current,size]");
        }

        return DocModel.builder()
                .desc(desc)
                .method(requestMethod)
                .result(result)
                .title(title)
                .url(url)
                .isPage(isPage)
                .pager(pager)
                .build();
    }

    /**
     * 参数对象创建
     * @param docParam 参数注解
     * @return 参数对象
     */
    public static ParamModel createParamModel(DocParam docParam) {
        String name = docParam.name();
        Required required = docParam.required();
        String desc = docParam.desc();
        String val = docParam.val();
        ParamType type = docParam.type();
        Class<?> clazz = docParam.clazz();

        ParamModel paramModel = new ParamModel();
        paramModel.setDesc(desc);
        paramModel.setName(name);
        paramModel.setRequired(required);
        paramModel.setVal(val);
        paramModel.setType(type);
        paramModel.setClazz(clazz);
        return paramModel;
    }
}
